package by.alex.dzeshko.facebooksuperclient.service;

import by.alex.dzeshko.facebooksuperclient.R;
import by.alex.dzeshko.facebooksuperclient.http.RequestBuilder;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public abstract class CommonService<T> extends Service {

	private static final String LOG_TAG = "CommonService";

	public static final String EXTRA_KEY_MESSAGE = "message";
	
	public static final String HTTP_REQUEST_BUILDER = "http_request_builder";
	
	public static final String NO_PAGES = "NO_PAGES";

	protected static final int TYPE = R.string.type;

	protected static final int MESSAGE = R.string.message;

	protected static final int ERROR = R.string._error;
	
	protected static final int NEXT = R.string.next;
	
	protected static final int DATA = R.string.data;

	protected static final int PAGING = R.string.paging;



	protected RequestBuilder requestBuilder;

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
	    handleCommand(intent);
	    return START_NOT_STICKY;
	}

	protected void handleCommand(Intent intent) {
		
		if (intent == null) {
			throw new IllegalArgumentException();
		}
		requestBuilder = (RequestBuilder) intent.getSerializableExtra(HTTP_REQUEST_BUILDER);
		if (requestBuilder == null) {
			Log.d(LOG_TAG,"no request builder");
			throw new IllegalArgumentException("No request builder.");
		}
	}
	
	protected void onHandleResponse(final T t){
		new Thread (new Runnable() {
			
			@Override
			public void run() {
				threadedHandleResponse(t);
			}
			
		}).start();
	}
	
	protected void threadedHandleResponse(T t) {

	}

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

}
